package vip.specimen.system.controller;

import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.datasource.entity.SystemAdvertisingEntity;
import vip.specimen.system.datasource.validation.SystemAdvertisingValidation;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.SystemAdvertisingService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/03/07 15:19
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Api(tags = "广告管理")
@RestController
@RequestMapping(value = "/advertising")
public class AdvertisingController {
    @Resource
    private SystemAdvertisingService advertisingService;

    @PutMapping
    @RequiresPermissions(value = "advertising:insert")
    @ApiOperation(value = "新增", notes = "新增广告")
    @SystemJurisdiction(resourceType = "button", permissionName = "添加广告")
    public JsonResult<SystemAdvertisingEntity> insert(@RequestBody @Valid SystemAdvertisingValidation ad) {
        return JsonResult.success(this.advertisingService.insert(ad));
    }

    @GetMapping
    @RequiresPermissions(value = "advertising:select")
    @ApiOperation(value = "查看", notes = "获取广告列表")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看广告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", dataType = "int")
    })
    public JsonResult<Page<SystemAdvertisingEntity>> findAll() {
        return JsonResult.success(this.advertisingService.findAll());
    }

    @PostMapping(value = "enable")
    @RequiresPermissions(value = "advertising:update:enable")
    @ApiOperation(value = "更新状态", notes = "根据uuid更新广告状态")
    @SystemJurisdiction(resourceType = "button", permissionName = "切换广告状态")
    public JsonResult<SystemAdvertisingEntity> update(@RequestParam("uuid") UUID uuid) {
        SystemAdvertisingEntity result = this.advertisingService.update(uuid);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }

    @PostMapping()
    @RequiresPermissions(value = "advertising:update")
    @ApiOperation(value = "更新", notes = "根据uuid更新广告")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新广告")
    public JsonResult<Object> update(@RequestParam("uuid") UUID uuid, @RequestBody @Valid SystemAdvertisingValidation ad) {
        SystemAdvertisingEntity result = this.advertisingService.update(uuid, ad);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }

    @DeleteMapping
    @RequiresPermissions(value = "advertising:delete")
    @ApiOperation(value = "删除广告", notes = "根据uuid删除广告")
    @SystemJurisdiction(resourceType = "button", permissionName = "删除广告")
    public JsonResult<Object> delete(@RequestBody HashMap<String, UUID> param) {
        return this.advertisingService.delete(param.get("uuid")) ? JsonResult.success() : JsonResult.failed(ResultCode.ERROR_DELETE);
    }

    @DeleteMapping(value = "batch")
    @RequiresPermissions(value = "advertising:delete:batch")
    @ApiOperation(value = "批量删除", notes = "根据uuids(UUID列表)删除广告")
    @SystemJurisdiction(resourceType = "button", permissionName = "批量删除广告")
    public JsonResult<HashMap<String, Integer>> deletes(@RequestBody HashMap<String, Set<UUID>> param) {
        int result = this.advertisingService.delete(param.get("uuids"));
        return JsonResult.success(new HashMap<String, Integer>(1) {{
            put("successNum", result);
        }});
    }
}
